package blockqueen;

import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Author ufo
 * @Description TODO
 * @Date 2021/1/21 10:47
 * @Version 1.0
 */
public class blockqueen {

    public static void main(String[] args) throws InterruptedException {

        blockqueen.test2();

    }


    //add remove 抛出异常
    public void test() {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.add("a");  //true
        blockingQueue.add("b");  //true
        blockingQueue.add("c");  //true
        System.out.println(blockingQueue.element());//队首
//        blockingQueue.add("d");    //异常停止Queue full
        System.out.println(blockingQueue.remove());//a
        System.out.println(blockingQueue.remove());//b
        System.out.println(blockingQueue.remove());//c
//        System.out.println(blockingQueue.remove());// 异常停止NoSuchElementException，


    }

    //offer poll 不抛出异常
    public static void test1() {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.offer("1"));//true
        System.out.println(blockingQueue.offer("2"));//true
        System.out.println(blockingQueue.offer("3"));//true
        System.out.println(blockingQueue.offer("4"));//false
        System.out.println(blockingQueue.peek()); //队首
        System.out.println(blockingQueue.poll());//1
        System.out.println(blockingQueue.poll());//2
        System.out.println(blockingQueue.poll());//3
        System.out.println(blockingQueue.poll());//null
    }

    //put take 阻塞
    // SynchronousQueue类似容量为1
    public static void test2() {
        SynchronousQueue<String> syn = new SynchronousQueue<>();

        new Thread(() -> {
            try {
                syn.put("1");  //SynchronousQueue容量为1，所以只能put一个，接下来就阻塞等待
                syn.put("2");
                syn.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                System.out.println(syn.take());  //take出一个元素
                TimeUnit.SECONDS.sleep(1);
                System.out.println(syn.take());
                TimeUnit.SECONDS.sleep(1);
                System.out.println(syn.take());
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    //offer() poll() 阻塞，超时等待
    public static void test3() throws InterruptedException {
        ArrayBlockingQueue a=new ArrayBlockingQueue(3);

        a.offer("a",2,TimeUnit.SECONDS);
        a.offer("b",2,TimeUnit.SECONDS);
        a.offer("c",2,TimeUnit.SECONDS);
        System.out.println("下面一行会停留2s");
        System.out.println(a.offer("d", 2, TimeUnit.SECONDS));
        System.out.println("下面一行会停留2s");
        System.out.println(a.offer("e", 2, TimeUnit.SECONDS));

        System.out.println(a.poll(3, TimeUnit.SECONDS));
        System.out.println(a.poll(3, TimeUnit.SECONDS));
        System.out.println(a.poll(3, TimeUnit.SECONDS));

        System.out.println("下面一行会停留3s");
        System.out.println(a.poll(3, TimeUnit.SECONDS));

        System.out.println("下面一行会停留3s");
        System.out.println(a.poll(3, TimeUnit.SECONDS));

        System.out.println("下面一行会停留3s");
        System.out.println(a.poll(3, TimeUnit.SECONDS));


    }



}
